<?php
// *** Tangra (Application Framework and Tools for PHP)
// $Id$
//

/**
 * Contains class Ajax_Form_Ctrl
 *
 * @package  modules
 * @subpackage  ajax_form_ctrl
 */

/**
 *
 */
require_once(TANGRA_MAIN_DIR.'form/form_ctrl.class.php');
/**
 *
 */
require_once(TANGRA_MAIN_DIR.'exceptions/tangra_module_exception.class.php');
/**
 *
 */
require_once($WSC->get_site_inc_dir().'modules/ajax_ctrl/ajax_ctrl.class.php');

/**
 * Base class for all ajax form controllers
 *
 */
abstract class Ajax_Form_Ctrl extends Ajax_Ctrl {
	/**
	 * form controller
	 *
	 * @var Form_Ctrl
	 * @internal
	 */
	protected $form_ctrl;


	/**
	 * Initialization
	 *
	 */
	protected function init() {
		$this->_create_form_ctrl();
	}


	/**
	 * Main method. Called by site instance
	 *
	 * Processes submit and sends back response
	 *
	 */
	public function run() {
		$context = $this->get__context();
		$rez = $this->form_ctrl->process_submit($context);

		$ret['result_code'] = Ajax_Ctrl::STATUS_OK;
		$html = '';

		switch ($rez) {
			case Form_Ctrl::SUBMIT_RESULT_OK:
				$additional_json_params = $this->on_good_submit();
				$this->add_additional_response_parameters($additional_json_params);
				break;
			case Form_Ctrl::SUBMIT_RESULT_ERROR:
				$additional_json_params = $this->on_bad_submit();
				$this->add_additional_response_parameters($additional_json_params);
				$this->form_ctrl->prepare_for_show();

				$html = $this->get_html();
				break;
			default:
				throw new Tangra_Module_Exception('Unexpected result code: '.$rez);
				break;
		}

		$this->send_response($ret, $html);
	}


	/**
	 * Returns reference to form controller
	 *
	 * @return Form_Ctrl
	 */
	public function get_form_ctrl() {
		return $this->form_ctrl;
	}


	/**
	 * Returns referece to form object from form controller
	 *
	 * @return Form
	 */
	public function get_form() {
		$form_ctrl = $this->get_form_ctrl();
		$form = $form_ctrl->get_form();

		return $form;
	}


	/**
	 * Returns form object from form controller
	 *
	 *
	 * @return Form
	 */
	public function query_form() {
		$form = $this->get_form();

		return $form;
	}


	/**
	 * Sets form controller
	 *
	 * @param Form_Ctrl $form_ctrl
	 * @internal
	 */
	protected function set_form_ctrl(Form_Ctrl $form_ctrl) {
		$this->form_ctrl = $form_ctrl;
	}


	/**
	 * Ensures that create_form_ctrl() is retirning object of class Form_Ctrl
	 *
	 * @internal
	 */
	protected function _create_form_ctrl() {
		$form_ctrl = $this->create_form_ctrl();
		if ($form_ctrl instanceof Form_Ctrl) {
			$this->set_form_ctrl($form_ctrl);
		} else {
			throw new Tangra_Module_Exception('$form_ctrl is not Form_Ctrl.');
		}
	}


	/**
	 * Returns generated HTML
	 *
	 * @return string
	 */
	protected function get_html() {
		$config = $this->get__config();

		if (file_exists($this->get_tpl_file()) && is_file($this->get_tpl_file())) {
			$ret = $this->_get_html($this->get_tpl_file());
		} else {
			$subdir = $this->get_tpl_file(); // we try to compose tpl file with treating tpl_file as subdir
			$form_name = strtolower($this->form_ctrl->get_form()->get_name());
			$file_path = $config->get_tpl_dir().$subdir.$form_name.'_form.tpl';

			if (file_exists($file_path)) {
				$ret = $this->_get_html($file_path);
			} else {
				throw new Tangra_Module_Exception('Cannot find form tpl file in "'.$this->get_tpl_file().'" or "'.$file_path.'"');
			}
		}


		return $ret;
	}


	/**
	 * Parses TPL file and returns HTML
	 *
	 * @param string $tpl_file Path to form tpl file
	 * @return string
	 */
	protected function _get_html($tpl_file) {
		$html_gatherer = new Tple_HTML_Gatherer($tpl_file);

		$this->add_exports($this->form_ctrl->get_tple_exports());

		$config = $this->get__config();
		$this->export('_language', $config->get_current_language());

		$ret = $html_gatherer->get_html($this->get__exports());

		return $ret;
	}


	/**
	 * Will be called if form is OK
	 *
	 */
	abstract protected function on_good_submit();

	/**
	 * Will be called if form contains errors
	 *
	 */
	abstract protected function on_bad_submit();


	/**
	 * User have to overload this method to create Form_Ctrl and return it
	 *
	 */
	abstract protected function create_form_ctrl();
}